<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class psetting extends CI_Controller {

	public function __construct()
    {
        parent::__construct();
        $all_session = $this->session->all_userdata();

    
        if($all_session['login'][0]['mem_type'] != 2 && $all_session['login'][0]['mem_type'] != 1)
        {
        	redirect('authen');
        } 

    }

    
	public function index()
	{
		$data['service'] = $this->psetting_model->get_web_service();

		$data['setting'] = $this->psetting_model->index();
		$data['setting_footer'] = $data['setting'];
		$data['count'] = count($data['setting']);

		$connection = $data['setting'];
		$data['connection'] = $this->connection($connection);

		$sum_vdofile = $this->psetting_model->sum_vdofile();
		$bytes = $sum_vdofile[0]['vdo_size'];
		$this->load->library('calculate_byte');
		$data['sum_vdofile'] = $this->calculate_byte->index($bytes);

		$this->load->view('psetting',$data);	
	}


	public function connection($connection) 
	{ 
		$online = 0;
		$offline = 0;

		for($i=0;$i<count(@$connection);$i++)
		{
			//////////////////////////
			//// test connectjion ////
			//////////////////////////
		    $conn = @fsockopen($connection[$i]['server_ip'], 80, $errno, $errstr, 1); 
		    if ($conn)
		    { 
		        $status[$i] = "Connected"; 
		        $online = $online + 1; 
		        fclose($conn);
		    }else{
		        $status[$i] = "Not connected";
		        $offline = $offline + 1;
		    }
		  	

		}// end loop
		$status['online'] = $online;
		$status['offline'] = $offline;
		return @$status;
	}

	public function setting()
	{
		$data['server'] = $this->psetting_model->index();

		$data['check_web'] = $this->psetting_model->get_web_service();
		if($data['check_web'] == '')
		{
			$web_service = array(
				'web_name' => $this->input->post('web_name')
			);
			$this->psetting_model->add_web_service($web_service);
		} else {
			$web_service = array(
				'web_name' => $this->input->post('web_name')
			);
			$this->psetting_model->update_web_service($web_service);
		}

		

		for($i=0;$i<count($data['server']);$i++){
			$input = array(
				'server_id' => $this->input->post('server_id_'.$i),
				'server_name' => $this->input->post('server_name_'.$i),
				'server_ip' => $this->input->post('server_ip_'.$i)
			);

			$server = $this->psetting_model->find_server($input);

			$all_session = $this->session->all_userdata();
			$log_file = array(						
				'log_url' => base_url().'index.php/psetting',
				'log_action' => 'Edit Streaming Server ['.$server[0]['server_ip'].' to '.$input['server_ip'].']',
				'log_target' => $input['server_name'],
				'log_status' => 'Pass',
				'mem_id' => $all_session['login'][0]['mem_id'],
				'log_ip' => $_SERVER['REMOTE_ADDR']						
			);
			$this->logfile_model->add_logfile($log_file);

			$this->psetting_model->update_stream_server($input);

		}

		


		redirect('psetting');

	}

	public function customlogo()
	{
		$path = strtolower($_FILES["userfile"]['name']);
		$file_type = pathinfo($path, PATHINFO_EXTENSION);

		if($file_type == 'jpeg' || $file_type == 'gif' || $file_type == 'png' || $file_type == 'jpg')
		{
			move_uploaded_file($_FILES["userfile"]["tmp_name"],"img/logo.".$file_type);
		}

		$web_service = array(
			'web_logo' => 'logo.'.$file_type
		);
		$this->psetting_model->update_web_service($web_service);
		redirect('psetting');

	}

	public function add_stream_server()
	{
		$input = array(
			'server_name' => $this->input->post('server_name'),
			'server_ip' => $this->input->post('server_ip'),
			'ftp_name' => $this->input->post('ftp_name'),
			'ftp_pass' => base64_encode($this->input->post('ftp_pass'))
		);
		$this->psetting_model->add_stream_server($input);

		$all_session = $this->session->all_userdata();
		$log_file = array(						
			'log_url' => base_url().'index.php/psetting',
			'log_action' => 'Add New Streaming Server',
			'log_target' => $input['server_ip'],
			'log_status' => 'Pass',
			'mem_id' => $all_session['login'][0]['mem_id'],
			'log_ip' => $_SERVER['REMOTE_ADDR']						
		);
		$this->logfile_model->add_logfile($log_file);

		redirect('psetting');
	}

	public function edit_stream_server()
	{
		$input = array(
			'server_id' => $this->input->post('server_id'),
			'server_name' => $this->input->post('server_name'),
			'server_ip' => $this->input->post('server_ip'),
			'ftp_name' => $this->input->post('ftp_name'),
			'ftp_pass' => base64_encode($this->input->post('ftp_pass'))
		);
		$server = $this->psetting_model->find_server($input);

		$all_session = $this->session->all_userdata();
		$log_file = array(						
			'log_url' => base_url().'index.php/psetting',
			'log_action' => 'Edit Streaming Server ['.$server[0]['server_ip'].' to '.$input['server_ip'].']',
			'log_target' => $input['server_name'],
			'log_status' => 'Pass',
			'mem_id' => $all_session['login'][0]['mem_id'],
			'log_ip' => $_SERVER['REMOTE_ADDR']						
		);

		$this->logfile_model->add_logfile($log_file);
		$this->psetting_model->update_stream_server($input);

		redirect('psetting');
	}

	public function remove_server()
	{
		$input = array(
			'server_id' => $this->input->post('server_id'),
		);		

		$data = $this->psetting_model->find_server($input);

		$all_session = $this->session->all_userdata();
		$log_file = array(						
			'log_url' => base_url().'index.php/psetting',
			'log_action' => 'Delete Streaming Server',
			'log_target' => $data[0]['server_ip'],
			'log_status' => 'Pass',
			'mem_id' => $all_session['login'][0]['mem_id'],
			'log_ip' => $_SERVER['REMOTE_ADDR']						
		);
		$this->logfile_model->add_logfile($log_file);
		$this->psetting_model->remove_server($input);

		redirect('psetting');
	}



}